package com.cby.library.tool.crash.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.cby.library.tool.R;
import com.cby.library.tool.crash.ui.listener.OnItemClickListener;
import com.cby.library.tool.util.SpanUtils;

import java.io.File;
import java.util.List;

/**
 * 奔溃信息列表的适配器
 *
 * @author chenbaoyang
 */
public class CrashInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private LayoutInflater layoutInflater;
    private List<File> fileList;
    private OnItemClickListener mOnItemClickListener;

    public CrashInfoAdapter(Context context, List<File> fileList) {
        this.context = context;
        this.fileList = fileList;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void updateDatas(List<File> fileList) {
        this.fileList = fileList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = layoutInflater.inflate(R.layout.item_crash_info_view, parent, false);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            File file = fileList.get(position);
            myViewHolder.pathTv.setText(file.getAbsolutePath());

            //动态修改颜色
            String fileName = file.getName().replace(".txt", "");
            String[] splitNames = fileName.split("_");
            Spannable spannable = null;
            if (splitNames.length == 3) {
                String errorMsgType = splitNames[2];
                if (!TextUtils.isEmpty(errorMsgType)) {
                    spannable = Spannable.Factory.getInstance().newSpannable(fileName);
                    spannable = SpanUtils.addNewSpan(context, spannable, fileName, errorMsgType,
                            Color.parseColor("#FF0006"), 0);
                }
            }
            if (spannable != null) {
                myViewHolder.titleTv.setText(spannable);
            } else {
                myViewHolder.titleTv.setText(fileName);
            }
            if (mOnItemClickListener != null) {
                myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mOnItemClickListener.onItemClick(view, position);
                    }
                });
                myViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        mOnItemClickListener.onLongClick(view, position);
                        return false;
                    }
                });
            }

        }
    }

    @Override
    public int getItemCount() {
        return fileList.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView titleTv;
        private TextView pathTv;

        public MyViewHolder(View itemView) {
            super(itemView);
            titleTv = itemView.findViewById(R.id.tv_title);
            pathTv = itemView.findViewById(R.id.tv_path);

        }
    }

}
